﻿@using System.Security.Claims
@using TalksLog.UI.Common
@using System.Linq;
@{
    ViewData["Title"] = "基本信息";
}

<div class="info_div">
    <div class="pure-g">
        <div class="pure-u-1-3"><span>邮箱</span></div>
        <div class="pure-u-1-3"><span>@Context.User.Claims.First(c => c.Type == ClaimTypes.Email).Value</span></div>
        <div class="pure-u-1-3"><span><a asp-action="Logout" asp-controller="Account" class="pure-button pure-button-primary">退出登录</a></span></div>
    </div>
    <div id="password_div" class="pure-g editable_info">
        <div class="pure-u-1-3"><span>密码</span></div>
        <div class="pure-u-1-3"><span>******</span></div>
    </div>
    <div class="hide_element">
        <div class="pure-form pure-form-aligned">
            <fieldset>
                <div class="pure-control-group">
                    <label for="OldPassword">旧密码</label>
                    <input name="OldPassword" id="OldPassword" type="password" placeholder="123QWE!%^">
                    <span id="old_password_error" class="field-validation-error"></span>
                </div>
                <div class="pure-control-group">
                    <label for="NewPassword">新密码</label>
                    <input name="NewPassword" id="NewPassword" type="password" placeholder="!QAZXSW#">
                    <span id="new_password_error" class="field-validation-error"></span>
                </div>
                <div class="pure-control-group">
                    <label for="ComfirmNewPassword">确认新密码</label>
                    <input name="ComfirmNewPassword" id="ComfirmNewPassword" type="password" placeholder="!QAZXSW#">
                    <span id="comfirm_new_password_error" class="field-validation-error"></span>
                </div>
                <div class="pure-controls">
                    <button id="save_password_button" class="pure-button pure-button-primary">修改</button>
                </div>
                <div><span id="password_error_sum" class="field-validation-error"></span></div>
                <div id="password_save_success" class="hide_element"><span class="import_info">修改成功，立即重新登录?&nbsp;<a asp-action="Logout" asp-controller="Account">登录</a></span></div>
            </fieldset>
        </div>
    </div>
    <div class="pure-g editable_info">
        <div class="pure-u-1-3"><span>昵称</span></div>
        <div class="pure-u-1-3"><span class="display_name">@Context.User.Claims.First(c => c.Type == ClaimTypes.Name).Value</span></div>
    </div>
    <div class="hide_element">
        <div class="pure-form pure-form-aligned">
            <fieldset>
                <div class="pure-control-group">
                    <label for="Name">新昵称</label>
                    <input name="Name" id="Name" type="text" placeholder="你超载了啊">
                    <span id="name_error" class="field-validation-error"></span>
                </div>
                <div class="pure-controls">
                    <button id="save_name_button" type="submit" class="pure-button pure-button-primary">修改</button>
                </div>
                <div><span id="name_error_sum" class="field-validation-error"></span></div>
                <div id="name_save_success" class="hide_element"><span class="import_info">修改成功</span></div>
            </fieldset>
        </div>
    </div>
    <div class="pure-g">
        <div class="pure-u-1-3"><span>性别</span></div>
        <div class="pure-u-1-3"><span>@Context.User.Claims.First(c => c.Type == ClaimTypes.Gender).Value</span></div>
    </div>
    <div class="pure-g">
        <div class="pure-u-1-3"><span>注册时间</span></div>
        <div class="pure-u-1-3"><span>@Context.User.Claims.First(c => c.Type == ClaimTypesExt.RegisterDate).Value</span></div>
    </div>
</div>

@section js{
    <script src="~/js/jquery/jquery-3.4.1.min.js"></script>
    <script>
        $(document).ready(function () {
            $(".info_div .editable_info").click(function () {
                $(this).next().toggle();
            });

            $("#save_password_button").click(function () {

                $("#old_password_error").text(null);
                $("#new_password_error").text(null);
                $("#comfirm_new_password_error").text(null);
                $("#password_error_sum").text(null);

                if (!$("#OldPassword").val()) {
                    $("#old_password_error").text("请输入旧密码");
                }
                else if (!$("#NewPassword").val()) {
                    $("#new_password_error").text("请输入新密码");
                }
                else if ($("#NewPassword").val() === $("#OldPassword").val()) {
                    $("#new_password_error").text("请输入和旧密码不同的新密码");
                }
                else if ($("#NewPassword").val().length < 6) {
                    $("#new_password_error").text("密码长度不能小于6位");
                }
                else if ($("#NewPassword").val() !== $("#ComfirmNewPassword").val()) {
                    $("#comfirm_new_password_error").text("两次密码输入不一致");
                } else {
                    $.post("/api/account/change_password", {
                        id: @Context.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value,
                        oldPassword: $("#OldPassword").val(),
                        newPassword: $("#NewPassword").val()
                    }, function (data, dataStatus) {
                        if (data) {
                            $("#password_save_success").show();
                            setTimeout(function(){$("#password_save_success").fadeOut(1000)},4000);
                        }
                        else {
                            $("#password_error_sum").text("请检查旧密码是否输入正确");
                        }
                    });
                }
            });

            $("#save_name_button").click(function () {

                $("#name_error").text(null);
                $("#name_error_sum").text(null);

                if (!$("#Name").val()) {
                    $("#name_error").text("请输入新昵称");
                }
                if ($("#Name").val().length < 2) {
                     $("#name_error").text("昵称长度至少需要2位")
                }
                else if ($("#Name").val() === "@Context.User.Claims.First(c => c.Type == ClaimTypes.Name).Value") {
                    $("#name_error").text("请输入和旧昵称不同的新昵称");
                }
                else {
                    $.post("/api/account/change_name", {
                        id: @Context.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value,
                        newName: $("#Name").val(),
                    }, function (data, dataStatus) {
                            if (data.isSuccess) {
                                $(".display_name").text($("#Name").val());
                                $("#name_save_success").show();
                                setTimeout(function () { $("#name_save_success").fadeOut(1000) }, 4000);
                            }
                            else {
                                $("#name_error_sum").text(data.message);
                            }
                    });
                }
            });
        });
    </script>
}

